<template>
    <div class="main-div">
        <!-- 顶部标题区 -->
        <header class="header">
            <div class="header-content">
                <h1 class="header-h1">智能作业分析</h1>
                <p class="header-p">基于AI技术的智能作业分析系统，为学习提供精准指导</p>
            </div>
        </header>
        <!-- 主要内容区 -->
        <main class="main-content">
            <!-- 步骤条 -->
            <el-steps :active="currentStep" align-center finish-status="success" class="steps-nav">
                <el-step 
                    v-for="(step, index) in steps" 
                    :key="index" 
                    :title="step.title"
                    @click="handleStepClick(index)"
                ></el-step>
            </el-steps>
            <!-- 内容区域 -->
            <div class="main-content-div">
                <!-- 作业分析 -->
                <div v-if="currentStep === 0">
                    <div class="work-analysis">
                        <div class="mb-8">
                            <div class="analysis-progress">
                                <span class="text-sm-text-gray-600">分析进度</span>
                                <!-- <span class="text-sm-text-blue-600">{{ analysisProgress }}%</span> -->
                            </div>
                            <el-progress :percentage="analysisProgress" :stroke-width="8"></el-progress>
                        </div>

                        <el-row :gutter="24" class="mb-8">
                            <el-col :span="6">
                                <el-card class="bg-blue-50">
                                    <div class="text-number">总题数</div>
                                    <div class="text-2xl-font-semibold">25 题</div>
                                </el-card>
                            </el-col>
                            <el-col :span="6">
                                <el-card class="bg-green-50">
                                    <div class="text-number">正确题数</div>
                                    <div class="text-2xl-font-semibold">18 题</div>
                                </el-card>
                            </el-col>
                            <el-col :span="6">
                                <el-card class="bg-red-50">
                                    <div class="text-number">错误题数</div>
                                    <div class="text-2xl-font-semibold">7 题</div>
                                </el-card>
                            </el-col>
                            <el-col :span="6">
                                <el-card class="bg-purple-50">
                                    <div class="text-number">正确率</div>
                                    <div class="text-2xl-font-semibold">72%</div>
                                </el-card>
                            </el-col>
                        </el-row>

                        <el-card class="mb-8">
                            <template #header>
                                <div class="font-medium">整体表现分析</div>
                            </template>
                            <p class="mb-8-title">
                                本次作业整体表现良好，基础知识掌握扎实。重点需要关注微积分部分，特别是导数应用题的解题思路和计算准确性。建议加强以下方面的练习：
                            </p>
                            <el-timeline class="mt-4">
                                <el-timeline-item type="success" size="large"
                                    icon="CircleCheck">函数与数列知识运用准确，计算过程规范</el-timeline-item>
                                <el-timeline-item type="warning" size="large"
                                    icon="Warning">立体几何空间想象和证明逻辑需要加强</el-timeline-item>
                                <el-timeline-item type="danger" size="large"
                                    icon="CircleClose">导数应用题解题思路不够清晰，计算有误</el-timeline-item>
                            </el-timeline>
                        </el-card>

                        <div class="space">
                            <el-card v-for="(item, index) in analysisResults" :key="index"
                                class="space-item">
                                <template #header>
                                    <div class="space-items-center">
                                        <div class="space-flex-items-center">
                                            <span class="items-center-font-medium">第 {{ item.number }} 题</span>
                                            <el-tag :type="item.correct ? 'success' : 'danger'" class="ml-3">
                                                {{ item.correct ? '正确' : '错误' }}
                                            </el-tag>
                                        </div>
                                        <div class="space-flex-items-text">
                                            <span class="items-center-1">
                                                <el-icon class="mr-1">
                                                    <Collection />
                                                </el-icon>
                                                知识点：{{ item.knowledge }}
                                            </span>
                                            <span class="items-center-2">
                                                <el-icon class="mr-1">
                                                    <Histogram />
                                                </el-icon>
                                                难度：{{ item.difficulty }}
                                            </span>
                                        </div>
                                    </div>
                                </template>

                                <!-- 添加题目显示部分 -->
                                <div class="question-content mb-4">
                                    <div class="question-text">{{ item.title?.text }}</div>
                                    <!-- 选择题选项 -->
                                    <template v-if="item.type === '选择题' && item.title?.options">
                                        <div class="options-list">
                                            <div v-for="(option, optIndex) in item.title.options" 
                                                 :key="optIndex" 
                                                 class="option-item">
                                                {{ option }}
                                            </div>
                                        </div>
                                    </template>
                                </div>

                                <div class="space-2">
                                    <div>
                                        <h4 class="space-2-text">解题分析</h4>
                                        <el-alert :title="item.analysis" type="info" :closable="false"></el-alert>
                                    </div>
                                    <div>
                                        <h4 class="space-2-tiShi">易错点提示</h4>
                                        <el-alert type="warning" :closable="false">
                                            <div class="space-2-warning">
                                                <div class="space-2-warning-start">
                                                    <el-icon class="space-2-warning-start-1">
                                                        <Warning />
                                                    </el-icon>
                                                    <span>注意{{ item.knowledge }}的基本性质应用，避免与其他概念混淆</span>
                                                </div>
                                                <div class="space-2-warning-start">
                                                    <el-icon class="space-2-warning-start-1">
                                                        <Lightning />
                                                    </el-icon>
                                                    <span>解题时应先列出已知条件，理清思路后再进行计算</span>
                                                </div>
                                            </div>
                                        </el-alert>
                                    </div>
                                    <div class="space-2-Explanation">
                                        <el-button type="primary" plain>
                                            <el-icon>
                                                <VideoCamera />
                                            </el-icon>查看视频讲解
                                        </el-button>
                                        <el-button type="success" plain>
                                            <el-icon>
                                                <List />
                                            </el-icon>练习相似题
                                        </el-button>
                                    </div>
                                </div>
                            </el-card>
                        </div>
                    </div>
                </div>

                <!-- 统计图表 -->
                <div v-if="currentStep === 1" id="chartContainer" class="h-[500px]"></div>

                <!-- 知识图谱 -->
                <div v-if="currentStep === 2" id="knowledgeMapContainer" class="h-[500px]">
                    <KnowledgeGraph />
                </div>

                <!-- 学习路径 -->
                <div v-if="currentStep === 3">
                    <div class="max-w-4xl mx-auto">
                        <h3 class="text-xl font-medium mb-6">个性化学习建议</h3>
                        <el-row :gutter="24">
                            <el-col :span="12">
                                <el-card class="mb-4">
                                    <template #header>
                                        <div class="font-medium">重点关注</div>
                                    </template>
                                    <el-timeline>
                                        <el-timeline-item v-for="(item, index) in focusPoints" :key="index"
                                            type="primary">
                                            {{ item }}
                                        </el-timeline-item>
                                    </el-timeline>
                                </el-card>

                                <el-card>
                                    <template #header>
                                        <div class="font-medium">建议复习</div>
                                    </template>
                                    <el-timeline>
                                        <el-timeline-item v-for="(item, index) in reviewPoints" :key="index"
                                            type="success">
                                            {{ item }}
                                        </el-timeline-item>
                                    </el-timeline>
                                </el-card>
                            </el-col>

                            <el-col :span="12">
                                <el-card>
                                    <template #header>
                                        <div class="font-medium">学习时间规划</div>
                                    </template>
                                    <el-timeline>
                                        <el-timeline-item v-for="(plan, index) in studyPlans" :key="index"
                                            :timestamp="plan.time" placement="top">
                                            <el-card>
                                                {{ plan.content }}
                                            </el-card>
                                        </el-timeline-item>
                                    </el-timeline>
                                </el-card>
                            </el-col>
                        </el-row>
                    </div>
                </div>

                <!-- 错题练习 -->
                <div v-if="currentStep === 4">
                    <div class="max-w-4xl mx-auto">
                        <div class="flex justify-between items-center mb-8">
                            <h3 class="text-xl font-medium">错题练习</h3>
                            <el-button type="primary">
                                <el-icon class="mr-2">
                                    <Printer />
                                </el-icon>打印练习
                            </el-button>
                        </div>

                        <el-row :gutter="24">
                            <el-col :span="12" v-for="(subject, index) in wrongExercises" :key="index">
                                <el-card>
                                    <template #header>
                                        <div class="flex justify-between items-center">
                                            <span class="font-medium">{{ subject.name }}</span>
                                            <el-tag>{{ subject.count }} 题</el-tag>
                                        </div>
                                    </template>
                                    <el-timeline>
                                        <el-timeline-item v-for="(exercise, idx) in subject.exercises" :key="idx"
                                            type="primary">
                                            <div class="flex justify-between items-center">
                                                <span>{{ exercise.title }}</span>
                                                <el-button type="primary" link>
                                                    <el-icon>
                                                        <RefreshRight />
                                                    </el-icon>
                                                </el-button>
                                            </div>
                                        </el-timeline-item>
                                    </el-timeline>
                                </el-card>
                            </el-col>
                        </el-row>
                    </div>
                </div>
            </div>

            <!-- 底部按钮 -->
            <div class="flex justify-between mt-8">
                <el-button :disabled="currentStep === 0" @click="prevStep">
                    上一步
                </el-button>
                <el-button type="primary" :disabled="currentStep === steps.length - 1" @click="nextStep">
                    下一步
                </el-button>
            </div>
        </main>
    </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, nextTick } from 'vue';
import * as echarts from 'echarts';
import {
    Upload,
    Delete,
    Collection,
    Histogram,
    Warning,
    Lightning,
    VideoCamera,
    List,
    Printer,
    RefreshRight
} from '@element-plus/icons-vue';
import KnowledgeGraph from './KnowledgeGraph.vue';

const currentStep = ref(0);
const analysisProgress = ref(85);
const chartInstance = ref<echarts.ECharts | null>(null);
const knowledgeMapInstance = ref<echarts.ECharts | null>(null);

const steps = [
    { title: '作业分析' },
    { title: '统计图表' },
    { title: '知识图谱' },
    { title: '学习路径' },
    { title: '错题练习' }
];

const uploadedFiles = ref([
    { name: '数学周测卷（一）.pdf' },
    { name: '物理作业20230615.jpg' }
]);

const analysisResults = [
    {
        number: 1,
        correct: true,
        answer: 'A',
        analysis: '运用二次函数的性质准确，求解最值的方法正确。图像绘制规范，计算过程清晰准确。特别是对函数零点的分析和判别式的使用都很到位。',
        knowledge: '二次函数',
        difficulty: '中等',
        note: '注意二次函数的基本性质应用，避免与其他概念混淆',
        trainThought: '注意训练思维，避免错误',
        videoLink: 'http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4',
        type: '选择题',
        title: {
            text: '操作系统中，被调度和分派资源的基本单位，并可独立执行的实体是()',
            options: ['A、线程', 'B、程序', 'C、进程', 'D、指令']
        }
    },
    {
        number: 2,
        correct: false,
        answer: '线程',
        analysis: '在求解复合函数导数时出现错误。需要注意链式法则的应用，同时对基本初等函数的求导公式掌握不够熟练。建议重点复习复合函数求导法则和常见函数的导数公式。',
        knowledge: '导数',
        difficulty: '较难',
        note:'注意导数的基本性质应用，避免与其他概念混淆',
        trainThought: '注意训练思维，避免错误',
        title: {
            text: '操作系统中，被调度和分派资源的基本单位，并可独立执行的实体是()',
            options: ['A、线程', 'B、程序', 'C、进程', 'D、指令']
        }
    },
    {
        number: 3,
        correct: true,
        answer: '线程',
        analysis: '数列通项公式的推导思路清晰，运用数学归纳法证明过程严谨。特别是在处理递推关系时，逻辑性强，表述准确。对等差数列和等比数列的性质理解深入。',
        knowledge: '数列',
        difficulty: '中等',
        note:'注意数列的基本性质应用，避免与其他概念混淆',
        trainThought: '注意训练思维，避免错误',
        title: {
            text: '操作系统中，被调度和分派资源的基本单位，并可独立执行的实体是()',
            options: ['A、线程', 'B、程序', 'C、进程', 'D、指令']
        }
    },
    {
        number: 4,
        correct: false,
        answer: '线程',
        analysis: '在解立体几何问题时，空间想象能力有待提升。对截面的判断不够准确，未能正确运用三视图辅助分析。建议多做此类型习题，加强空间思维训练。',
        knowledge: '立体几何',
        difficulty: '较难',
        note:'',
        title: {
            text: '操作系统中，被调度和分派资源的基本单位，并可独立执行的实体是()',
            options: ['A、线程', 'B、程序', 'C、进程', 'D、指令']
        }
    },
    {
        number: 5,
        correct: true,
        answer: '线程',
        analysis: '概率计算思路正确，能够准确区分排列组合的使用场景。在处理几何概率时，对样本空间的划分和有利情况的判断都很准确。',
        knowledge: '概率统计',
        difficulty: '中等',
        note:'',
        title: {
            text: '操作系统中，被调度和分派资源的基本单位，并可独立执行的实体是()',
            options: ['A、线程', 'B、程序', 'C、进程', 'D、指令']
        }
    }
];

const focusPoints = [
    '导数的应用需要加强练习',
    '立体几何的空间想象能力需要提升',
    '数列通项公式的推导需要巩固'
];

const reviewPoints = [
    '复习二次函数的性质和应用',
    '整理数列常见题型和解法',
    '复习三角函数的基本公式'
];

const studyPlans = [
    { time: '9:00-10:30', content: '复习导数章节重点内容' },
    { time: '10:45-12:00', content: '练习立体几何典型题目' },
    { time: '14:00-15:30', content: '数列专项训练' },
    { time: '15:45-17:00', content: '错题订正与知识点总结' }
];

const wrongExercises = [
    {
        name: '数学',
        count: 5,
        exercises: [
            { title: '导数应用题 - 最值问题' },
            { title: '立体几何 - 截面问题' },
            { title: '数列 - 通项公式' }
        ]
    },
    {
        name: '物理',
        count: 4,
        exercises: [
            { title: '动量守恒定律应用' },
            { title: '电磁感应现象' },
            { title: '机械能转换问题' }
        ]
    }
];

const handleUpload = (file: any) => {
    // 模拟文件上传
    uploadedFiles.value.push({
        name: file.name
    });
};

const removeFile = (index: number) => {
    uploadedFiles.value.splice(index, 1);
};

const prevStep = () => {
    if (currentStep.value > 0) {
        currentStep.value--;
    }
};

const nextStep = () => {
    if (currentStep.value < steps.length - 1) {
        currentStep.value++;
    }
};

const initChart = () => {
    const chartDom = document.querySelector('#chartContainer');
    if (!chartDom) return;
    chartInstance.value = echarts.init(chartDom as HTMLElement);
    const option = {
        animation: false,
        title: {
            text: '知识点掌握情况统计',
            left: 'center'
        },
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            data: ['正确率', '错误率'],
            bottom: 0
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '10%',
            containLabel: true
        },
        xAxis: {
            type: 'category',
            data: ['函数', '导数', '数列', '立体几何', '概率统计']
        },
        yAxis: {
            type: 'value',
            max: 100
        },
        series: [
            {
                name: '正确率',
                type: 'bar',
                data: [85, 65, 78, 72, 88],
                itemStyle: {
                    color: '#60A5FA'
                }
            },
            {
                name: '错误率',
                type: 'bar',
                data: [15, 35, 22, 28, 12],
                itemStyle: {
                    color: '#F87171'
                }
            }
        ]
    };
    chartInstance.value.setOption(option);
};

const initKnowledgeMap = () => {
    const mapDom = document.querySelector('#knowledgeMapContainer');
    if (!mapDom) return;
    knowledgeMapInstance.value = echarts.init(mapDom as HTMLElement);
    const option = {
        animation: false,
        title: {
            text: '知识点关联图谱',
            left: 'center'
        },
        tooltip: {},
        legend: {
            data: ['已掌握', '待加强', '待学习'],
            bottom: 0
        },
        series: [{
            type: 'graph',
            layout: 'force',
            data: [
                { name: '函数', value: 85, category: 0, symbolSize: 50 },
                { name: '导数', value: 65, category: 1, symbolSize: 45 },
                { name: '数列', value: 78, category: 0, symbolSize: 48 },
                { name: '几何', value: 72, category: 1, symbolSize: 46 },
                { name: '统计', value: 88, category: 0, symbolSize: 52 }
            ],
            categories: [
                { name: '已掌握' },
                { name: '待加强' },
                { name: '待学习' }
            ],
            force: {
                repulsion: 100,
                edgeLength: 100
            },
            edges: [
                { source: '函数', target: '导数' },
                { source: '导数', target: '数列' },
                { source: '几何', target: '函数' },
                { source: '统计', target: '数列' }
            ]
        }]
    };
    knowledgeMapInstance.value.setOption(option);
};

// 添加步骤点击处理函数
const handleStepClick = (index: number) => {
    currentStep.value = index;
};

onMounted(async () => {
    await nextTick();
    initChart();
    initKnowledgeMap();
});
</script>

<style scoped>
.el-card {
    border-radius: 8px;
}

.el-timeline-item__node {
    background-color: var(--el-color-primary);
}

.el-alert {
    border-radius: 8px;
}

.el-upload {
    width: 100%;
}

.el-upload-dragger {
    width: 100%;
    height: 200px;
}
.main-div {
    min-height: 100vh;
    background-color: #F9FAFB;
}
.header {
    background-color: #ffffff;
    box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
}
.header-content {
    padding-top: 1.5rem;
    padding-bottom: 1.5rem;
    padding-left: 2rem;
    padding-right: 2rem;
    max-width: 80rem;
    margin: 0 auto;
}
.header-h1 {
    font-size: 1.875rem;
    line-height: 2.25rem;
    font-weight: 700;
    color: #111827;
}
.header-p {
    margin-top: 0.5rem;
    color: #4B5563;
}
.main-content {
    padding-left: 2rem;
    padding-right: 2rem;
    padding-top: 2.5rem;
    padding-bottom: 2.5rem;
    max-width: 80rem;
    margin: 0 auto;
}
.main-content-div {
    padding: 2rem;
    border-radius: 0.5rem;
    background-color: #ffffff;
    box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    margin-top: 2rem;
    margin-bottom: 2rem;
}
.main-content-div-el-icon{
    margin-bottom: 1rem; 
}
.mt-8{
    margin-top: 2rem; 
}
.text-left {
    margin-bottom: 1rem;
    font-weight: 500;
    text-align: left;
}
.work-analysis{
    max-width: 48rem;
    margin: 0 auto;
}
.mb-8{
    margin-bottom: 2rem; 
}
.font-medium{
    font-weight: 500; 
}
.analysis-progress {
    display: flex;
    margin-bottom: 0.5rem;
    justify-content: space-between;
    align-items: center;
}
.text-sm-text-gray-600{
    font-size: 0.875rem;
line-height: 1.25rem; 
color: #4B5563; 
}
.text-sm-text-blue-600 {
    font-size: 0.875rem;
    line-height: 1.25rem;
    color: #2563EB;
}
.bg-blue-50{
    background-color: #EFF6FF; 
}
.bg-green-50{
    background-color: #ECFDF5; 
}
.bg-red-50{
    background-color: #FEF2F2; 
}
.bg-purple-50{
    background-color: #F5F3FF; 
}
.text-number {
    margin-bottom: 0.25rem;
    font-size: 0.875rem;
    line-height: 1.25rem;
    color: #4B5563;
}
.text-2xl-font-semibold {
    font-size: 1.5rem;
    line-height: 2rem;
    font-weight: 600;
}
.mb-8-title {
    line-height: 1.625;
    color: #374151;
}
.mt-4{
    margin-top: 1rem; 
}
.space{
    margin-top: 1.5rem; 
}
.space-item {
    transition-property: box-shadow;
    transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
    transition-duration: 300ms;
    margin-bottom: 2rem;
}
.space-item:last-child {
    margin-bottom: 0;
}
.space-item:hover {
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}
.space-items-center {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.space-flex-items-center {
    display: flex;
    align-items: center;
}
.items-center-font-medium {
    font-size: 1.125rem;
    line-height: 1.75rem;
    font-weight: 500;
}
.ml-3{
    margin-left: 0.75rem; 
}
.space-flex-items-text {
    display: flex;
    align-items: center;
    font-size: 0.875rem;
    line-height: 1.25rem;
    color: #6B7280;
}
.items-center-1 {
    display: flex;
    margin-right: 1rem;
    align-items: center;
}
.mr-1{
    margin-right: 0.25rem; 
}
.items-center-2 {
    display: flex;
    align-items: center;
}
.space-2{
    margin-top: 1rem; 
}
.space-2-text {
    margin-bottom: 0.5rem;
    font-size: 0.875rem;
    line-height: 1.25rem;
    font-weight: 500;
    color: #374151;
}
.space-2-tiShi {
    margin-bottom: 0.5rem;
    font-size: 0.875rem;
    line-height: 1.25rem;
    font-weight: 500;
    color: #374151;
}
.space-2-warning{
    margin-top: 0.5rem; 
}
.space-2-warning-start {
    display: flex;
    align-items: flex-start;
}
.space-2-warning-start-1 {
    margin-top: 0.25rem;
    margin-right: 0.5rem;
}
.space-2-Explanation {
    display: flex;
    margin-left: 1rem;
    justify-content: flex-end;
    margin-top: 1rem;
}
.steps-nav :deep(.el-step__title) {
    cursor: pointer;
}

.steps-nav :deep(.el-step__head) {
    cursor: pointer;
}

.steps-nav :deep(.el-step) {
    cursor: pointer;
}

.steps-nav :deep(.el-step):hover .el-step__title {
    color: var(--el-color-primary);
}

.question-content {
    background-color: #F9FAFB;
    padding: 1rem;
    border-radius: 0.5rem;
    margin-bottom: 1rem;
}

.question-text {
    font-size: 1rem;
    line-height: 1.5rem;
    color: #1F2937;
    margin-bottom: 0.75rem;
}

.options-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.option-item {
    padding: 0.5rem;
    background-color: white;
    border: 1px solid #E5E7EB;
    border-radius: 0.375rem;
    font-size: 0.875rem;
    color: #374151;
}
</style>